﻿<!DOCTYPE html>
<html>
<head>
    <script src="/js/header.js"></script>
</head>
<body class="bg_white">
    <section class="container-fluid" style="background-color: white;">
        <form class="form-horizontal m15_t" id="form">
            <div class="tab-content">
                <div class="tab-pane fade in active">
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>类型：</label>
                        <div class="wd350 m15_l">
                            <select class="form-control" name="type">
                                <option value="">请选择</option>
                                <option value="1">存款(主)</option>
                                <option value="2">存款(副)</option>
                                <option value="3">付款(主)</option>
                                <option value="4">付款(副)</option>
                                <option value="5">中转(主)</option>
                                <option value="6">中转(副)</option>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>顺序：</label>
                        <div class="wd350 m15_l">
                            <input class="form-control" type="text" name="sortNo" maxlength="2" placeholder="只能输入整数" value="0" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>推送接收地址：</label>
                        <div class="wd350 m15_l">
                            <input class="form-control" type="text" name="url" maxlength="200" placeholder="以http(s)://开头" />
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label">推送版本：</label>
                        <div class="wd300 m15_l">
                            <label class="m5_t">V</label>
                            <label class="m5_t" style="margin-left:-3px;" id="version"></label>
                        </div>
                    </div>

                    <div class="form-group" id="divPushDeposit">
                        <label class="wd100 control-label">收款记录：</label>
                        <div class="wd350 m15_l">
                            <label class="m10_r">
                                <input type="radio" name="pushDeposit" value="2" checked="checked" /> 不推送
                            </label>
                            <label>
                                <input type="radio" name="pushDeposit" value="1" /> 推送
                            </label>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>状态：</label>
                        <div class="wd350 m15_l">
                            <input type="radio" name="status" value="1" checked="checked" />启用
                            <input type="radio" name="status" value="2" />禁用
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="wd100 control-label">描述：</label>
                        <div class="wd350 m15_l">
                            <textarea style="height: auto;font-size: 12px;width: 100%;" id="description" name="remark" rows="6" maxlength="120"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </section>
    <script type="text/javascript">
        var validator = null;
        $(function () {
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                showInfo(id);
            } else {
                var type = FlashPay.Util.GetQueryString("type");
                removeSlt(type);
                let version = FlashPay.Util.GetQueryString("version");
                $("#version").text(version);
            }
            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    type: {
                        required: true,
                    },
                    sortNo: {
                        required: true,
                        IsPositiveInteger: true
                    },
                    url: {
                        required: true
                    }
                    , description: {
                        specialChar: true
                    }
                },
                messages: {
                    type: {
                        required: "请选择一种类型"
                    },
                    sortNo: {
                        required: "请输入顺序号",
                        IsPositiveInteger: "请输入整数"
                    },
                    url: {
                        required: "请输入推送接收地址"
                    },
                    description: {
                        specialChar: "描述不能带有特殊字符"
                    }
                }
            });
        });

        //显示值
        function showInfo(id) {
            var cover = FlashPay.UI.Mask({
                obj: $("body"),
                opacity: 0.5
            });
            FlashPay.Util.Ajax({
                type: "get",
                url: "/Company/GetExtApiPushUrl",
                data: { id: id },
                success: function (data) {
                    if (data.id > 0) {
                        FlashPay.Util.SetInputVal("sortNo", data.sortNo);
                        FlashPay.Util.SetInputVal("url", data.url);
                        $("#description").val(data.description);
                        removeSlt(data.type);
                        $("select[name='type']").find("option[value=" + data.type + "]").prop("selected", true);
                        $("input[name='status']").get(data.status-1).checked = true;
                        $("#version").text(data.version);

                        if (data.pushDeposit == 0) {
                            $("#divPushDeposit").remove();
                        } else {
                            $("input[name='pushDeposit']").get(data.pushDeposit > 1 ? 0 : data.pushDeposit).checked = true;//收款记录
                        }
                    }
                    cover.Remove();
                },
                error: function (json) {
                    cover.Remove();
                }
            });
        }

        function removeSlt(type) {
            if (type <= 2) {
                removeNumArr([3, 4, 5, 6]); $("#divPushDeposit").remove();
            } else if (type == 3 || type == 4) {
                removeNumArr([1, 2, 5, 6]);
            }
            else {
                removeNumArr([1, 2, 3, 4]);
            }
        }

        function removeNumArr(arr) {
            $.each(arr, function (e, k) {
                $("select[name='type'] option[value='"+k+"']").remove();
            })
        }

        function save() {
            if (!validator.form()) {
                return false;
            }
            var model = {};
            model.Type = $("select[name='type']").val();
            model.SortNo = $('input[name="sortNo"]').val();
            model.Url = $("input[name='url']").val();
            model.Description = $("#description").val();
            model.Status = $("input[name='status']:checked").val();

            if (FlashPay.Util.GetQueryString("type") != "1") {
                model.PushDeposit = $("input[name='pushDeposit']:checked").val();
            }
            model.Version = $("#version").text();

            if (!FlashPay.Util.IsURL(model.Url)) {
                FlashPay.UI.Tip_short_danger("请输入正确的推送接收地址");
                return;
            }

            var url = "/Company/ExtApiPushUrlAddOrEdit";
            var id = FlashPay.Util.GetQueryString("id");
            if (id != null && id != undefined) {
                model.ID = id;
            }
            var companyID = FlashPay.Util.GetQueryString("companyID");
            model.CompanyID = companyID;

            if ($("body").data("confirmReceive") == "lock") {
                FlashPay.UI.Tip_short_danger("操作过快");
                return false;
            } else {
                $("body").data("confirmReceive", "lock");
            }
            FlashPay.Util.Ajax({
                type: "POST",
                url: url,
                data: model,
                success: function (data) {
                    $("body").data("confirmReceive", "unlock");
                    if (!data.success) {
                        if (data.errorCode == "100" || data.errorCode == "500") {
                            return;
                        } else {
                            FlashPay.UI.Tip_short_warning(data.errorMessage == null ? "操作失败！" : data.errorMessage);
                            return;
                        }
                    } else {
                        FlashPay.UI.Tip_success(data.successMessage == null ? "操作成功！" : data.successMessage);
                        let type = model.Type;
                        if (type <= 2) {
                            FlashPay.UI.DialogOpener().doSearch(0);
                        } else if(type==3 || type==4) {
                            FlashPay.UI.DialogOpener().doSearch(1);
                        } else  {
                            FlashPay.UI.DialogOpener().doSearch(2);
                        }
                        setTimeout(function () {
                            FlashPay.UI.CloseDialog();
                        }, 1000);
                    }
                }
            });
        }
    </script>
</body>
</html>